package cn.wit.summit.chwan.widgets;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.google.gson.Gson;

import java.util.List;

import cn.wit.summit.chwan.R;
import cn.wit.summit.chwan.Stat.AdPosition;
import cn.wit.summit.chwan.Stat.StatFactory;
import cn.wit.summit.chwan.models.db.DownLoadTaskTable;
import cn.wit.summit.chwan.models.dbmanager.DownloadTaskManager;
import cn.wit.summit.chwan.models.dto.GameListBaseBean;
import cn.wit.summit.chwan.models.dto.GameTagInfoBean;
import cn.wit.summit.chwan.models.response.IndexBeanV3Response;
import cn.wit.summit.chwan.tools.downloadtool.DownloadFactory;
import cn.wit.summit.chwan.utils.IntentUtil;
import cn.wit.summit.chwan.utils.UtilsMy;

/**
 * Created by Alan on 2018/3/13.
 */

public class ItemListPartView extends LinearLayout implements View.OnClickListener {

    private Gson mGson = null;
    private Context mContext = null;
    private DownloadFactory factory;
    private LayoutParams params;
    private Uri uri;
    private DownLoadTaskTable mDownLoadTaskTable = null;
    private ItemDownloadInterface mItemDownloadInterface;

    private RelativeLayout itemDownloadRootRl;
    private SimpleDraweeView itemDownloadImg; //任务图片
    private TextView itemDownloadNameTx;      //任务名字
    private TextView itemDownloadSizeTx;      //热度和下载大小
    private TextView itemDownloadMoneyTx;      //
    private LinearLayout itemDownloadDescLl;  //任务简介布局，非下载的时候显示
    private LinearLayout itemDownloadTagsLl;  //任务标签布局，会动态加入标签
    private TextView itemDownloadDescTx;      //任务简介
    private LinearLayout itemTagInfoLl;   //任务标签
    private DownloadViewStroke itemDownloadBtn;       //任务控制按钮

    private Drawable drawable;
    private IndexBeanV3Response.CreditInfoBean creditInfoBean;

    public ItemListPartView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context, attrs);
    }

    public ItemListPartView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context, attrs);
    }

    private void initView(Context context, AttributeSet attrs) {
        this.mContext = context;
        mGson = new Gson();
        uri = Uri.parse("res:///" + R.drawable.main_normal_icon);
        params = new LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        params.setMargins(5, 0, 5, 0);
        params.gravity = Gravity.CENTER;
        factory = DownloadFactory.get();
        drawable = mContext.getResources().getDrawable(R.mipmap.conner_tip);

        View rootView = LayoutInflater.from(context).inflate(R.layout.item_download_part_view, null);
        itemDownloadRootRl = rootView.findViewById(R.id.itemDownloadRootRl);
        itemDownloadImg = rootView.findViewById(R.id.itemDownloadImg);
        itemDownloadNameTx = rootView.findViewById(R.id.itemDownloadNameTx);
        itemDownloadSizeTx = rootView.findViewById(R.id.itemDownloadSizeTx);
        itemDownloadDescLl = rootView.findViewById(R.id.itemDownloadDescLl);
        itemDownloadTagsLl = rootView.findViewById(R.id.itemDownloadTagsLl);
        itemDownloadDescTx = rootView.findViewById(R.id.itemDownloadDescTx);
        itemDownloadMoneyTx = rootView.findViewById(R.id.itemDownloadMoneyTx);
        itemTagInfoLl = rootView.findViewById(R.id.itemTagInfoLl);

        itemDownloadBtn = rootView.findViewById(R.id.itemDownloadBtn);

        itemDownloadBtn.setOnClickListener(this);
        itemDownloadRootRl.setOnClickListener(this);
        itemDownloadRootRl.setOnLongClickListener(new OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (mItemDownloadInterface != null) {
                    mItemDownloadInterface.onAdapterItemLongChildClick(v, mDownLoadTaskTable);
                    return true;
                } else {
                    return false;
                }
            }
        });

        this.removeAllViews();
        this.addView(rootView);
    }

    GameListBaseBean gameListBaseBean;
    Activity activity;

    public void setData(Activity activity, GameListBaseBean gameListBaseBean) {
        this.activity = activity;
        setData(gameListBaseBean, true);
    }

    public void setData(Activity activity, GameListBaseBean gameListBaseBean, boolean showTagInfo) {
        this.activity = activity;
        setData(gameListBaseBean, showTagInfo);
    }

    private int isFrom;//1:首页
    private int position;
    public void setData(Activity activity, GameListBaseBean gameListBaseBean, boolean showTagInfo, int isFrom, int position) {
        this.activity = activity;
        this.isFrom = isFrom;
        this.position = position;
        setData(gameListBaseBean, showTagInfo);
    }

    public void setDataNotagInfo(GameListBaseBean gameListBaseBean) {
        setData(gameListBaseBean, false);

    }

    public void setData(GameListBaseBean gameListBaseBean, boolean showTagInfo) {
        this.gameListBaseBean = gameListBaseBean;
        itemDownloadImg.setImageURI(gameListBaseBean.getGame_ico());
        itemDownloadNameTx.setText(gameListBaseBean.getGame_name());
        itemDownloadDescTx.setText(gameListBaseBean.getGame_info());
        itemDownloadSizeTx.setText(gameListBaseBean.getGame_down_count() + "/" + gameListBaseBean.getGame_size() + "M");
        if (showTagInfo) {
            itemTagInfoLl.setVisibility(VISIBLE);
            itemDownloadDescTx.setVisibility(GONE);
            UtilsMy.addTipsInView(gameListBaseBean.getGame_tag_info_v2(), itemTagInfoLl, getContext());
        } else {
            itemTagInfoLl.setVisibility(GONE);
            itemDownloadDescTx.setVisibility(VISIBLE);
            itemDownloadDescTx.setText(gameListBaseBean.getGame_info());
        }
        DownLoadTaskTable taskTable = gameListBaseBean.getTaskTable();
        if (taskTable == null) {
            taskTable = gameListBaseBean.getDownloadTaskTableSimple();

            taskTable = DownloadTaskManager.getInstance().findByGameId(gameListBaseBean.getGame_id());
        }
        itemDownloadBtn.initData(gameListBaseBean);

        creditInfoBean = gameListBaseBean.getCredit_info();
        if (creditInfoBean!=null && creditInfoBean.getDay_surplus()>0
                && creditInfoBean.getWeek_surplus()>0
                && creditInfoBean.getGame_down_load_reward()>0
                && (taskTable==null || taskTable.getDownStatus()==0)) {
            itemDownloadMoneyTx.setVisibility(VISIBLE);
            itemDownloadMoneyTx.setText("+ "+ creditInfoBean.getGame_down_load_reward() +"元");
        } else {
            itemDownloadMoneyTx.setVisibility(GONE);
        }

        List<GameTagInfoBean> gameTagInfoBeans=gameListBaseBean.getGame_tag_info();
        if (gameTagInfoBeans!=null){
            for (GameTagInfoBean bean:gameTagInfoBeans){
                if (bean.getId().equals("25")){
                    itemDownloadImg.getHierarchy().setOverlayImage(drawable);
                    break;
                } else {
                    itemDownloadImg.getHierarchy().setOverlayImage(null);
                }
            }
        }
    }

    public DownloadViewStroke getDownView() {
        return itemDownloadBtn;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.itemDownloadRootRl://item点击
                IntentUtil.getInstance().goGamedetial(activity, gameListBaseBean.getGame_id());
                if (isFrom == 1 && position>0) {
                    StatFactory.getInstance().sendClickAdvertSpacePage(mContext, AdPosition.getModeByEnum(5)
                            , AdPosition.getNameByEnum(5), "", "", position);
                }
                break;
        }
    }

    public void setOnclickLinstener(ItemListPartView.ItemDownloadInterface itemDownloadInterface) {
        mItemDownloadInterface = itemDownloadInterface;
    }

    public interface ItemDownloadInterface {
        void onAdapterItemLongChildClick(View view, DownLoadTaskTable taskTable);
    }
}
